package com.hctforgreen.greenservice.model;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.hctforgreen.greenservice.utils.HctConstants;

/**
 * 6.12、全部试题信息（按权限）
 * @author Tallen Zhai
 *
 */
public class AllExamListEntity extends BaseEntity {

	private static final long serialVersionUID = -2613073625753371584L;

	public List<ExamEntity> dataList = null;
	public String state = "";
	
	public final static String STATE = "state";
	private final static String BODY = "body";
	
	public static AllExamListEntity parse(JSONObject jsonObject)throws JSONException{
		AllExamListEntity bean = new AllExamListEntity();
		if(jsonObject.has(STATE)){
			bean.state = jsonObject.getString(STATE);
		}
		/**
		 * 解析body节点
		 */
		if (jsonObject.has(BODY)) {
			
			JSONArray jsonArray = jsonObject.getJSONArray(BODY);
				
			bean.dataList = new ArrayList<ExamEntity>();
			for (int i = 0; i < jsonArray.length(); i++) {
				JSONObject object = jsonArray.getJSONObject(i);
				ExamEntity entity = ExamEntity.parse(object);
				bean.dataList.add(entity);
			}
			
		}
		/**
		 * -1表示:无数据
		 */
		if(bean.state.equals(HctConstants.RESULT_STATE_FAILD)
				&& bean.dataList == null){
			bean.dataList = new ArrayList<ExamEntity>();
		}
		return bean;
	}
	/**
	 * 习题
	 * @author Tallen Zhai
	 *
	 */
	public static class ExamEntity extends BaseEntity {

		private static final long serialVersionUID = 5762303378487399407L;
		public String id = "";
		public String subject = "";
		public String offerAnswer = "";
		public String rightAnswer = "";
		public String hint = "";
		public String point = "";
		/**
		 * 1:单选or多选
		 * 2:判断
		 * 3:填空
		 */
		public String qtype = "";
		public String repType = "";
		public String repid = "";
		
		public String answerNum = "";
		/**
		 * 1:单选
		 * 2:多选
		 */
		public String choiseType = "";
		//===========仅在页面中用到==========
		/**
		 * 答案选项
		 */
		public List<String> offerAnswerList = null;
		public String wrongAnswer = "";
		/**
		 * 错误答案
		 */
		public List<String> wrongAnswerList = null;
		/**
		 * 正确答案
		 */
		public List<String> rightAnswerList = null;
		public String inputAnswer = "";
		/**
		 * 刚刚输入的答案
		 */
		public List<String> inputAnswerList = null;
		/**
		 * 全对、半对、全错、""为未做
		 */
		public String inputAnswerResult = "";
		/**
		 * 得分
		 */
		public double inputAnswerScore = 0;
		
		public final static String ID = "id";
		public final static String SUBJECT = "subject";
		public final static String OFFER_ANSWER = "offeranswer";
		public final static String ANSWER = "answer";
		public final static String HINT = "hint";
		public final static String POINT = "point";
		
		public final static String Q_TYPE = "qtype";
		public final static String REP_TYPE = "repType";
		public final static String REP_ID = "repid";
		public final static String ANSWER_NUM = "answerNum";
		public final static String CHOISE_TYPE = "choiseType";
		
		public static ExamEntity parse(JSONObject jsonObject)
				throws JSONException {
			ExamEntity bean = new ExamEntity();
			if (jsonObject.has(ID)) {
				bean.id = jsonObject.getString(ID);
			}
			
			if (jsonObject.has(SUBJECT)) {
				bean.subject = jsonObject.getString(SUBJECT);
			}
			if (jsonObject.has(OFFER_ANSWER)) {
				/**
				 * 过滤掉中文空格、英文空格
				 */
				bean.offerAnswer = jsonObject.getString(OFFER_ANSWER).replace("　", "").replace(" ", "");
				
			}
			
			if (jsonObject.has(ANSWER)) {
				bean.rightAnswer = jsonObject.getString(ANSWER);
			}
			if (jsonObject.has(HINT)) {
				bean.hint = jsonObject.getString(HINT);
			}
			if (jsonObject.has(POINT)) {
				bean.point = jsonObject.getString(POINT);
			}
			
			if (jsonObject.has(Q_TYPE)) {
				bean.qtype = jsonObject.getString(Q_TYPE);
			}
			if (jsonObject.has(REP_TYPE)) {
				bean.repType = jsonObject.getString(REP_TYPE);
			}
			if (jsonObject.has(REP_ID)) {
				bean.repid = jsonObject.getString(REP_ID);
			}
			
			if (jsonObject.has(ANSWER_NUM)) {
				bean.answerNum = jsonObject.getString(ANSWER_NUM);
			}
			if (jsonObject.has(CHOISE_TYPE)) {
				bean.choiseType = jsonObject.getString(CHOISE_TYPE);
			}
			
			return bean;
		}
	}
}
